Site hosted by Angelfire.com: Build your free website today!

Chapter 4 - Variables, Data Types and Arithmetic Expressions

List of Questions


Back to Home Page

Question 1

Type in and run the five programs presented in this chapter. Compare the output produced by each program with the output presented after each program in the text.

Answer 1

Basic instruction...

Back to Top

Question 2

Which of the following are invalid variable names? Why?


Int           char     6_05
Calloc        Xx       alpha_beta_routine
floating      _1312    z
ReInitialize  _        A$

Answer 2

Since all variable names can only start with lowercase letters or an underscore and contain only letters (upper and lower case), numbers and underscores and cannot be reserved words, the following names are invalid:


Int
Calloc
ReInitialize
char
Xx
6_05
A$

Back to Top

Question 3

Which of the following are invalid constants? Why?


123.456      0x10.5    0X0G1 
0001         0xFFFF    123L
0Xab05       0L        -597.25
123.5e2      .0001     +12
98.6F        98.7U     17777s
0996         -12E-12   07777
1234uL       1.2Fe-7   15,000
1.234L       197u      100U
0XABCDEFL    0xabcu    +123

Answer 3

Since all constants can start with or contain letters (upper and lower case) and underscores, cannot start with numbers but can contain numbers, cannot contain spaces or punctuation marks and cannot be reserved words, the following names are invalid:


0996
0x10.5
98.7U
1.2Fe-7
0X0G1
17777s
15,000

Back to Top

Question 4

Write a program that converts 27° from degrees Fahrenheit (F) to degrees Celsius (C) using the following formula:

C = (F - 32) / 1.8


Answer 4


/*Chapter 4 Exercise 4 (4.Ex4)*/

#include <stdio.h>

int main (void)
{

	int   fahr = 27;
	float result;


	result = (fahr - 32) / 1.8;
	printf ("%i degrees Fahrenheit is equal to %g in degrees Celsius.\n", fahr, result);

	return 0;
}
Back to Top

Question 5

What output would you expect from the following program?


/*Chapter 4 Exercise 5*/

#include <stdio.h>

int main (void)
{
    char c, d;
    
    c = 'd';
    d = c;
    printf ("d = %c\n", d);
    
    return 0;
}

Answer 5

The result is d = d (Followed by a blank line)

Back to Top

Question 6

Write an equation to evaluate the polynomial shown here:

3x3 - 5x2 + 6

for x = 2.55

Answer 6


/* Chapter 4 Exercise 6 (4.Ex6)*/

#include <stdio.h>

int main (void)
{

	float result;
	
	result = 3*pow(2.55,3) - 5*pow(2.55,2) + 6;
    
    printf ("\ny = 3x^3 - 5x^2 + 6\n\n");
    printf ("Where x = 2.55\n\n");
    printf ("y = %g\n", result);
    
	
	return 0;
}
Back to Top

Question 7

Write a program that evaluates the following expression and displays the results (remember to use the exponential format to display the result):

(3.31 × 10-8 × 2.01 × 10-7) / (7.16 × 10-6 + 2.01 × 10-8)

Answer 7


/*Chapter 4 Exercise 7 (4.Ex7)*/

#include <stdio.h>

int main (void)
{
    double d1 = 3.31*pow(10,-8) * 2.01*pow(10,-7);
    double d2 = 7.16*pow(10,-6) + 2.01*pow(10,-8);
    float result;
    
    result = d1/d2;
    
    printf ("\n(3.31 * 10^-8  *  2.01 * 10^-7) / ");
    printf ("(7.16 * 10^-6  +  2.01 * 10^-8)\n\n");
    printf ("The result is %g\n", result);
    
    return 0;
}   
Back to Top

Question 8

To round off an integer i to the next largest even multiple of another integer j, the following formula can be used:

Next_Multiple = i + j - i % j

Write a program to find the next largest even multiple for the following values of i and j:

i j

365 7

12,258 23

996 4

Answer 8


/*Chapter 4 Exercise 8 (4.Ex8)*/

#include <stdio.h>

int main (void)
{

    int Next_Multiple, result;
    
    int i1 = 365, i2 = 7;                  //Part 1
    Next_Multiple = i1 + i2 - i1 % i2;       
    printf ("For i = 365\n");
    printf ("The next multiple divisible by 7 is %i\n", Next_Multiple);
    result = Next_Multiple / i2;
    printf ("%i divided by %i equals %i\n\n", Next_Multiple, i2, result);
   
    i1 = 12258, i2 = 23;                   //Part 2
    Next_Multiple = i1 + i2 - i1 % i2;      
    printf ("For i = 12,258\n");
    printf ("The next multiple divisible by 23 is %i\n", Next_Multiple);
    result = Next_Multiple / i2;
    printf ("%i divided by %i equals %i\n\n", Next_Multiple, i2, result);
    
    i1 = 996, i2 = 4;                     //Part 3 NB i1 is already divisible by i2
    Next_Multiple = i1 + i2;                
    printf ("For i = 996\n");
    printf ("The next multiple divisible by 4 is %i\n", Next_Multiple);
    result = Next_Multiple / i2;
    printf ("%i divided by %i equals %i\n\n", Next_Multiple, i2, result);
    
    return 0;
}    

Back to Top

Back to Home Page